package com.wangzy.myairobot.serial;

import android.app.Application;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.wangzy.myairobot.App;

import java.util.HashMap;
import java.util.Iterator;

public class UsbDeviceHelper {
    private UsbManager usbManager;
    private Context context;

    public UsbDeviceHelper(Context context) {
        this.context = context;
        this.usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    }

    public void listUsbDevices() {

        if (usbManager == null) {
            Log.e(App.tag, "UsbManager is not available");
            return;
        }
        // 获取连接的USB设备列表
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();

        Log.d(App.tag,"usb 设备列表数量:"+deviceList.size());

        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            // 获取设备信息
            Log.d(App.tag, "Device Name: " + device.getDeviceName());
            Log.d(App.tag, "Device ID: " + device.getDeviceId());
            Log.d(App.tag, "Vendor ID: " + device.getVendorId());
            Log.d(App.tag, "Product ID: " + device.getProductId());
        }
    }
}
